package hms.inventory;

import hms.database.DAO;
import hms.master.MasterController;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.DefaultListModel;

/**
 * Sizes is a collection of inventory sizes
 * 
 * 
 * @author Akilah McIntyre
 * 
 */
public class Sizes extends DefaultListModel {
	public Size invSize;

	/**
	 * // TODO [Akilah] Add Comments
	 */
	public Sizes() {
	}

	/**
	 * Initialize sizes from database
	 */
	public void initSizes() {
		int size_id = -1; // unique identifier
		String size_name = ""; // name
        String size_count = "select count(*) from SIZE where size_id =99 and SIZE_NAME='UNDEFINED'";
        int undfCount = 0;
        try {
        	ResultSet rsCount = MasterController.getDatabaseObject().getResult(size_count);
        	while(rsCount.next()){
        		undfCount = rsCount.getInt(1);
        		if(undfCount == 0){
        			MasterController.getDatabaseObject().updateCommand("INSERT INTO SIZE(SIZE_ID,SIZE_NAME) VALUES(99,'UNDEFINED')");
        		}
        	}
		// get data from database associated with dao
		ResultSet rsSizes = MasterController.getDatabaseObject().getResult(
				"SELECT SIZE_ID, SIZE_NAME FROM SIZE WHERE SIZE_ID != 99 ORDER BY SIZE_ID");
		
			while (rsSizes.next()) {
				size_id = rsSizes.getInt("SIZE_ID");
				size_name = rsSizes.getString("SIZE_NAME");
				Size size_item = new Size(size_id, size_name); // Create
																// Size
																// instance
																// from
				this.addElement(size_item); // add to sizes model
			}
		} catch (SQLException e) {
			// TODO [Akilah] Write to view or popup dialog
			e.printStackTrace();
		}
	}

	/**
	 * Add inventory size
	 * 
	 * @param next_size_id
	 * @param new_size_name
	 */
	public void addSize(int next_size_id, String new_size_name) {
		this.addElement(new Size(next_size_id, new_size_name));
	}

	/**
	 * Add inventory size
	 * 
	 * @param new_size_name
	 */
	public boolean addDbSize(String new_size_name) {
		boolean isSuccess = false;
		// Get new id as new id may not be +1
		int next_size_id = MasterController.getDatabaseObject()
				.addDBTableValueParam("SIZE", "SIZE_NAME", "SIZE_ID",
						new_size_name);
		if (next_size_id > -1) { // if the insertion was successful
			this.addElement(new Size(next_size_id, new_size_name));
			isSuccess = true;
		}

		return isSuccess;
	}

	/**
	 * @param size_name
	 *            New Name of Size
	 * @param selIndex
	 *            Index of target size
	 * @return if database update is success
	 */
	public boolean updateDbSize(String size_name, int selIndex) {
		boolean isSuccess = false;
		int size_id = -1;
		try {
			size_id = ((Size) this.get(selIndex)).size_id;
			isSuccess = MasterController.getDatabaseObject()
					.updateDBTableValueParam("SIZE", "SIZE_NAME", "SIZE_ID",
							size_name, size_id);
			if (isSuccess) {
				this.changeSizeName(size_name, selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param size_name
	 *            New Name of Size
	 * @param selIndex
	 *            Index of target size
	 */
	public void changeSizeName(String size_name, int selIndex) {
		Size destSize = ((Size) this.get(selIndex));
		destSize.size_name = size_name;
	}

	public boolean deleteDbSize(String size_name, int selIndex) {
		boolean isSuccess = false;
		int size_id = -1;
		try {
			size_id = ((Size) this.get(selIndex)).size_id;
			MasterController.getDatabaseObject().updateCommand("UPDATE INVENTORY SET SIZE_ID = 99 WHERE SIZE_ID = " + size_id);
			isSuccess = MasterController.getDatabaseObject()
					.updateCommand("DELETE FROM SIZE WHERE SIZE_ID = " + size_id);
			if (isSuccess) {
				this.deleteSizeName(selIndex);
			}
		} catch (Exception ex) {
			isSuccess = false;
		}
		return isSuccess;
	}

	/**
	 * @param selIndex
	 *            Index of target size
	 */
	public void deleteSizeName(int selIndex) {
		this.remove(selIndex);
		
	}
}

